/*
 * OLED_drives.c
 *
 *  Created on: 2021年8月12日
 *      Author: EE
 */
#include "i2c.h"
#include "dma.h"
#include "stm32f1xx_hal_i2c.h"
#include "OLED_drives.h"
#include "font.h"

uint8_t CMD_Data[] = { 0xAE, 0x00, 0x10, 0x40, 0x81, 0xCF, 0xA1, 0xC8, 0xA6,
		0xA8, 0x3F, 0xD3, 0x00, 0xD5, 0x80, 0xD9, 0xF1, 0xDA, 0x12, 0xdB, 0x40,
		0x20, 0x02, 0x8D, 0x14, 0xA4, 0xA6, 0xAF };

uint8_t CMD_Data1[] = { 0xAE, 0x00, 0x10, 0x40, 0xB0, 0x81, 0xFF, 0xA1, 0xA6,
		0xA8, 0x3F, 0xC8, 0xD3, 0x00, 0xD5, 0x80, 0xD8, 0x05, 0xD9, 0xF1, 0xDA,
		0x12, 0xDB, 0x30, 0x8D, 0x14, 0xAF, 0x20, 0x00 };

void OLED_Init(void) {
	uint8_t i;

	for (i = 0; i < 28; i++)
		HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
				&CMD_Data[i], 1);
	OLED_Clear();

	SetCurSorAddress(0, 0);
	HAL_Delay(200);
}

//清除OLED
void OLED_Clear(void) {
	uint8_t i, j;

	for (i = 0; i < 8; i++) {
		HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
				(uint8_t*) 0xB0 + i, 1);
		HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
				(uint8_t*) 0x00, 1);
		HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
				(uint8_t*) 0x10, 1);
		for (j = 0; j < 128; j++)
			HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
					(uint8_t*) 0x00, 1);
		//HAL_Delay(100);
	}
}

void OLED_PageClear(uint8_t Page) {
	uint8_t i;

	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) 0xB0 + Page, 1);
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) 0x00, 1);
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) 0x10, 1);
	for (i = 0; i < 128; i++)
		HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
				(uint8_t*) 0x00, 1);
}

//设置光标位置
//X 0 ~ 127
//Y 0 ~ 7
void SetCurSorAddress(uint8_t x, uint8_t y) {
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) 0xB0 + y, 1);
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) (((x & 0xF0) >> 4) | 0x10), 1);
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) (x & 0x0F), 1);
}

void OLED_Address(uint8_t row, uint8_t col) {
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) 0xB0 + row, 1);
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) (0x10 + ((col & 0xf0) >> 4)), 1);
	HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
			(uint8_t*) (0x00 + (col & 0x0F)), 1);
}

//version	1.1
void DisplayString(uint8_t x, uint8_t y, uint8_t width, uint8_t height,
		uint8_t *Str) {
	uint8_t addr = 0, i;
//uint16_t font = 0;

	while (*Str != '\0') {
		if (*Str >= ' ' && *Str <= '~') {
			addr = *Str - ' ';

			SetCurSorAddress(x, y);
			for (i = 0; i < width / 2; i++)
				HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
						(uint8_t*) &ASCII_8_16[addr][i], 1);

			SetCurSorAddress(x, y + 1);
			for (i = 0; i < width / 2; i++)
				HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
						(uint8_t*) &ASCII_8_16[addr][i + width / 2], 1);
			Str++;
			x += width / 2;
		}
	}
}

//version	1.2
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t CharSize) {

	uint8_t c = 0, i;

	c = chr - ' ';
	if (x > 128 - 1) {
		x = 0;
		y += 2;
	}
	if (CharSize == 16) {
		SetCurSorAddress(x, y);
		for (i = 0; i < 8; i++)
			HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
					(uint8_t*) &F8X16[c * 16 + i], 1);
		SetCurSorAddress(x, y + 1);
		for (i = 0; i < 8; i++)
			HAL_I2C_Mem_Write_DMA(&hi2c2, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
					(uint8_t*) &F8X16[c * 16 + i + 8], 1);
	}
}

void ShowString(uint8_t x, uint8_t y, uint8_t *Str, uint8_t CharSize) {

	uint8_t j = 0;

	while (Str[j] != '\0') {
		OLED_ShowChar(x, y, Str[j], CharSize);
		x += 8;
		if (x > 120) {
			x = 0;
			y += 2;
			j++;
		}
	}
}
